package com.zhu.tool_test.controller;




import com.linktopa.framework.common.model.response.ResponseResult;
import com.zhu.tool_test.entity.Device;
import com.zhu.tool_test.response.DataFactory;
import com.zhu.tool_test.response.Messages;
import com.zhu.tool_test.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ggBall
 * @since 2020-12-27
 */
@RestController
@RequestMapping("/device")
@Slf4j
public class DeviceController {

    @Autowired
    IDeviceService deviceService;

    @GetMapping("/devices")
    public ResponseResult devices(){

        List<Device> list = null;
        try {

             list = deviceService.allDevices();
        }catch (Exception e) {
            e.printStackTrace();
            log.error("设备查询失败");
            return DataFactory.createError().include(Messages.KEY_MSG,"设备查询失败").build();
        }
        return DataFactory.createSuccess().include(Messages.KEY_DATA,list).build();

    }

    @PostMapping("/save-single")
    public ResponseResult saveDevice(){
        try {
            Device device = new Device();
            device.setMn("xxxxceshi");
            device.setAlarmDetail("测试");
            device.setSampleTime(new Date());
            deviceService.insert(device);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("设备新增失败");
            return DataFactory.createError().include(Messages.KEY_MSG,"设备新增失败").build();
        }

        return DataFactory.createSuccess().include(Messages.KEY_MSG,"设备新增成功").build();


    }

    @DeleteMapping
    public ResponseResult deleteDevice(Long id){
        Device byId = deviceService.getById(id);
        deviceService.deleteById(id);
        return DataFactory.createSuccess().include(Messages.KEY_MSG,"设备:{"+id+"},删除成功").build();

    }

}
